<?php

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use App\Http\Service\web\SystemGroupService;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class SystemStoreGroupController extends Controller
{

    /**
     * 分组列表
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function GroupList()
    {
        try {
            $data = SystemGroupService::groupList();
            return $data;
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 添加分组
     * @return \Illuminate\Http\JsonResponse
     */
    public function addGroup()
    {
        $name = \request()->post('name');
        $status = \request()->post('status');
        $add_time = date("Y-m-d H:i:s");
        try {
            $data = SystemGroupService::addGroup($name, $status, $add_time);
            if ($data) {
                Redis::lpush("log", "$data->id", json_encode($data));
            }
            return parent::success('添加成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse|int
     */
    public function groupDel($id)
    {
        try {
            $data = SystemGroupService::groupDelService($id);
            return parent::success('删除成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }

    }

    /**
     * 分组修改
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function groupUpdate($id, Request $request)
    {

        $data = [
            'name' => $request->input('name'),
            'status' => $request->input('status'),
            'add_time' => date('Y-m-d H:i:s')
        ];
        try {
             SystemGroupService::updateService($id, $data);
            return parent::success('修改成功');
        } catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }

    }
}
